{{!
Copyright (c) 2023 Oracle and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
}}{{#header}}{{.}}
{{/header}}
package {{packageName}};

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

import io.helidon.common.types.Annotation;
import io.helidon.common.types.TypeName;
import io.helidon.common.types.TypedElementInfo;
import io.helidon.common.types.TypeValues;
import io.helidon.inject.api.ClassNamed;
import io.helidon.inject.api.InvocationContext;
import io.helidon.inject.api.Interceptor;
import io.helidon.inject.api.InvocationException;
import io.helidon.inject.api.ServiceProvider;
import io.helidon.inject.runtime.InterceptedMethod;

import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Provider;
import jakarta.inject.Singleton;

import static io.helidon.common.types.TypeName.create;
import static io.helidon.inject.runtime.Invocation.createInvokeAndSupply;
import static io.helidon.inject.runtime.Invocation.mergeAndCollapse;

/**
 * Injection {@link Interceptor} for {@link {{parent}} }.
 */
// using the interfaces approach
@io.helidon.common.Weight({{weight}})
@io.helidon.inject.api.Intercepted({{parent}}.class)
@Singleton
@SuppressWarnings("ALL")
{{{generatedanno}}}
public class {{className}} /* extends {{parent}} */ implements {{interfaces}} {
    private static final List<Annotation> __serviceLevelAnnotations = List.of({{#servicelevelannotations}}
            {{{.}}}{{#unless @last}},{{/unless}}{{/servicelevelannotations}});
{{#interceptedmethoddecls}}
    private static final TypedElementInfo __{{id}} = TypedElementInfo.builder()
            {{{.}}}
            .build();{{/interceptedmethoddecls}}

    private static final TypeName __serviceTypeName = TypeName.create({{parent}}.class);

    private final Provider<{{parent}}> __provider;
    private final ServiceProvider<{{parent}}> __sp;
    private final {{parent}} __impl;{{#interceptedelements}}
    private final List<Provider<Interceptor>> __{{id}}__interceptors;{{/interceptedelements}}{{#interceptedelements}}
    private final InterceptedMethod<{{parent}}, {{elementTypeName}}> __{{id}}__call;{{/interceptedelements}}

    @Inject
    @SuppressWarnings("unchecked")
    {{this.className}}({{#annotationtriggertypenames}}
                @ClassNamed({{{.}}}.class) List<Provider<Interceptor>> {{id}},{{/annotationtriggertypenames}}
                Provider<{{parent}}> provider) {
        this.__provider = Objects.requireNonNull(provider);
        this.__sp = (provider instanceof ServiceProvider) ? (ServiceProvider<{{parent}}>) __provider : null;
        {{#ctorinterceptedelements}}List<Provider<Interceptor>> __ctor__interceptors = mergeAndCollapse({{interceptedTriggerTypeNames}}{{#unless @last}}, {{/unless}});{{/ctorinterceptedelements}}{{#interceptedelements}}
        this.__{{{id}}}__interceptors = mergeAndCollapse({{interceptedTriggerTypeNames}});{{/interceptedelements}}

        Function<Object[], {{parent}}> call = args -> __provider.get();
        {{parent}} result = createInvokeAndSupply(
                InvocationContext.builder()
                    .serviceProvider(__sp)
                    .serviceTypeName(__serviceTypeName)
                    .classAnnotations(__serviceLevelAnnotations)
                    .elementInfo(__ctor)
                    .interceptors(__ctor__interceptors)
                    .build(),
                call,
                new Object[0]);
        this.__impl = Objects.requireNonNull(result);{{#interceptedelements}}

        this.__{{id}}__call = new InterceptedMethod<{{parent}}, {{elementTypeName}}>(
                __impl, __sp, __serviceTypeName, __serviceLevelAnnotations, __{{id}}__interceptors, __{{id}}{{elementArgInfo}}) {
            @Override
            public {{elementTypeName}} invoke(Object... args) throws Throwable {
                {{#if hasReturn}}return impl().{{id}}({{objArrayArgs}});{{else}}impl().{{id}}({{objArrayArgs}});
                return null;{{/if}}
            }
        };{{/interceptedelements}}
    }
{{#interceptedelements}}
    @Override
    {{{methodDecl}}} {
        Object[] args = new Object[] { {{args}} };
        {{#if hasReturn}}return {{/if}}createInvokeAndSupply(__{{id}}__call.ctx(), __interceptedArgs -> __{{id}}__call.apply(__interceptedArgs), args);
    }
{{/interceptedelements}}
}
